//
//  EmojiView.m
//  MiBaoTu
//
//  Created by admin on 16/12/8.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "EmojiView.h"

@interface EmojiView ()<UIScrollViewDelegate> {
    UIPageControl           *pageCtrl;
}

@property (nonatomic,strong)NSArray    *picArr;

@end
@implementation EmojiView

- (instancetype)init
{
    self = [super initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 216)];
    if (self) {
        [self onCreateUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self onCreateUI];
    }
    return self;
}

#pragma mrak - UI
- (void)onCreateUI
{
    
    self.alpha = 0.;
    self.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 216);
    self.backgroundColor = UIColorFromHex(0xf5f5f5);
    
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
    scrollView.backgroundColor = [UIColor clearColor];
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.delegate = self;
    [self addSubview:scrollView];
    
    pageCtrl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, scrollView.height-30, scrollView.width, 20)];
    pageCtrl.pageIndicatorTintColor = [UIColor whiteColor];
    pageCtrl.currentPageIndicatorTintColor = TINK_BG_COLOR;
    pageCtrl.currentPage = 0;
    [self addSubview:pageCtrl];
    [self insertSubview:pageCtrl aboveSubview:scrollView];
    
    //数据
    NSString *picPath = [[NSBundle mainBundle]pathForResource:@"EmojiAll" ofType:@"plist"];
    _picArr = [[NSMutableArray alloc]initWithContentsOfFile:picPath];

    if (_picArr.count > 0) {
        int h_count = (WIDTH_5S ? 7 : 8);
        int l_count = 4;
        
        float item_ju = 15;
        float item_width = 1. * (SCREEN_WIDTH-(h_count+1)*item_ju) / h_count;
        float item_height = 1. * (self.height-30-(l_count+1)*item_ju) / l_count;
        
        int page = 0; //页数

        for (int i=0 ; i < _picArr.count;i++) {
            
            NSString *item = [_picArr objectAtIndex:i];
            if ([item isKindOfClass:[NSString class]]) {
                if (item.length > 0) {
                    //画图
                    int h = i / h_count; //第几行
                    int l = i % h_count; //第几列
                    
                    //当前版填满
                    page = h / l_count;
                    h = h % l_count;
                    
                    CGRect frame = CGRectMake(item_ju+(item_ju+item_width)*l+page*SCREEN_WIDTH, item_ju+(item_ju+item_height)*h, item_width, item_height);
                    
                    UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
                    bt.frame = frame;
                    [bt setTitle:item forState:UIControlStateNormal];
                    [bt setTitle:item forState:UIControlStateSelected];
                    bt.titleLabel.font = [UIFont systemFontOfSize:15.];
                    bt.backgroundColor = [UIColor clearColor];
                    bt.tag = 1000+i;
                    [bt addTarget:self action:@selector(onSelectEmojiAction:) forControlEvents:UIControlEventTouchUpInside];
                    [scrollView addSubview:bt];
                }
            }
        }
        scrollView.contentSize = CGSizeMake(SCREEN_WIDTH*(page+1), scrollView.height);
        pageCtrl.numberOfPages = page + 1;
    }
    
}

- (void)onSelectEmojiAction:(UIButton *)bt
{
    NSInteger index = bt.tag - 1000;
    
    if (index < _picArr.count && index >= 0) {
        if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemForEmojiView:withView:)]) {
            [_delegate onSelectItemForEmojiView:[_picArr objectAtIndex:index] withView:self];
        }
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int index = (int)(scrollView.contentOffset.x / scrollView.width);
    pageCtrl.currentPage = index;
}

@end
